//
//  NSString+hexStringToData.m
//  UDKLib
//
//  Created by idiot.lin on 15/3/11.
//  Copyright (c) 2015年 biao.rao. All rights reserved.
//

#import "NSString+hexStringToData.h"

@implementation NSString (hexStringToData)
-(NSData *)hexToByteToNSData
{
    int j=0;
    int len = [self length];
    
    Byte bytes[len/2];
    for(int i=0;i<len;i++)
    {
        int int_ch;  ///两位16进制数转化后的10进制数
        unichar hex_char1 = [self characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
        i++;
        unichar hex_char2 = [self characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            int_ch2 = hex_char2-87; //// a 的Ascll - 97
        int_ch = int_ch1+int_ch2;
        bytes[j] = int_ch;  ///将转化后的数放入Byte数组里
        j++;
    }
    //    for (int i = 0; i<str.length/2; i++) {
    //        NSLog(@"%d",bytes[i]);
    //    }
    NSData *newData = [[NSData alloc] initWithBytes:bytes length:len/2 ];
    return newData;
}

-(NSData *)stringToHexStringToByteToNSDataForCharacteristic2
{
    NSData *myD = [self dataUsingEncoding:NSUTF8StringEncoding];
    //String to hex
    Byte *bytes1 = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes1[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    
    hexStr = [hexStr stringByAppendingString:@"0D0A"];
    
    int j=0;
    int len = [hexStr length];
    
    Byte bytes[len/2];
    for(int i=0;i<len;i++)
    {
        int int_ch;  ///两位16进制数转化后的10进制数
        unichar hex_char1 = [hexStr characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
        i++;
        unichar hex_char2 = [hexStr characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            int_ch2 = hex_char2-87; //// a 的Ascll - 97
        int_ch = int_ch1+int_ch2;
        bytes[j] = int_ch;  ///将转化后的数放入Byte数组里
        j++;
    }
    //    for (int i = 0; i<str.length/2; i++) {
    //        NSLog(@"%d",bytes[i]);
    //    }
    NSData *newData = [[NSData alloc] initWithBytes:bytes length:len/2 ];
    return newData;
}

- (NSString *)hexString
{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSString *str = @"";
    char *buffer = (char *)data.bytes;
    for (int i = 0;i<data.length;i++)
    {
        str = [str stringByAppendingFormat:@"%2X",buffer[i]];
    }
    return str;
}

@end









